from django.contrib.syndication.feeds import Feed
from models import BlogPost
from django.core.urlresolvers import reverse
from ragendja.dbutils import get_object_list, get_object
from bvuserapp.models import User
from django.contrib.syndication.feeds import FeedDoesNotExist
from django.core.exceptions import ObjectDoesNotExist
import logging
class UserEntries(Feed):
    
    description = ""
    
    def get_object(self, user):
        if len(user) != 1:
            raise ObjectDoesNotExist
        return get_object(User, 'username = ', user[0]) 


    def title(self, obj):
        return "BioVenTurist last entries in %s blog." % obj.username

    def link(self, obj):

        if not obj:
            raise FeedDoesNotExist
    
        #return reverse('blog_index', kwargs={'username': obj.username })
        return '/'

    def items(self, obj):
        logging.debug(obj)
        return get_object_list(BlogPost, "author = ", obj, 'is_private = ', False).order("-created_at").fetch(10)



class LatestEntries(Feed):
    title = "BioVenTurist last entries in user blogs"
    link = '/analysis/'
    description = ""

    def items(self):        
        return get_object_list(BlogPost, 'is_private = ', False).order('-created_at').fetch(10)
